package com.cuit.food.food.Yservice;

import com.cuit.food.Usercomment;
import com.cuit.food.food.Ydao.usercommentrepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class UserCommentservice {
    @Autowired
    usercommentrepository Usercommentrepository;

    public List<Usercomment> findAll() {
        return Usercommentrepository.findAll();
    }


    /**
     * 获得商店id得到的评分
     * @param id
     * @return
     */
    public double GetRestScore(String id) {
        List<Usercomment> userComment = Usercommentrepository.findAllByResId(id);
        if(userComment.size() == 0) return 0;
        double tot = 0;
        for(Usercomment usercomment : userComment) {
            tot += Double.parseDouble(usercomment.getScore());
        }
        return tot/userComment.size();
    }


    public  int GetRestCommentCount(String id) {
        return Usercommentrepository.findAllByResId(id).size();
    }

    public List<Usercomment> findAllbyrestid(String id) {
        return Usercommentrepository.findAllByResId(id);
    }

    /**
     *
     * @param id 获得饭店id对应用户的1-2个评论
     * @return
     */
    public ArrayList<String> FindrescommentByid(String id) {
        List<Usercomment> usc = Usercommentrepository.findAllByResId(id);
        List<String> result = new ArrayList<>();

        for(Usercomment usercomment : usc) {
            result.add(usercomment.getComment());
        }

        //返回1-2个就行了
        Collections.sort(result);
        if(result.size() >= 2) {
            return new ArrayList<String>(){
                {
                    add(result.get(0));
                    add(result.get(1));
                }
            };
        }
        else if(result.size() == 1) {
            return new ArrayList<String>(){
                {
                    add(result.get(0));
                }
            };
        }
        else return new ArrayList<>();
    }
}
